package main

import (
	"context"
	"fmt"
	"time"
)

func chan1() {
	var c1 chan int
	// 长度未0 容量为0
	fmt.Printf("c1: %d, %d, %v\n", len(c1), cap(c1), c1) // c1: 0, 0, <nil>
	// 长度为0 要阻塞
	//c1 <- 111
	<-c1
}

var c2 = make(chan int)

func chan2() {
	//容量为0的非缓冲通
	fmt.Printf("c1: %d, %d, %v\n", len(c2), cap(c2), c2)
	// 没有消费者这里会阻塞
	c2 <- 2
}

func tit() {
	ticker := time.NewTicker(2 * time.Second)
	go func() {
		for {
			t := <-ticker.C
			fmt.Println(t)
		}
	}()
}

func main() {
	//go chan2()
	// ta := time.After(2 * time.Second)
	// t1 := <-ta
	// fmt.Println(t1)
	// data := <-c2
	// fmt.Println("取到数据", data)
	//select {}

	// c3 := make(chan int, 8)
	// c3 <- 1
	// c3 <- 2
	// close(c3)

	// for v := range c3 {
	// 	fmt.Println(v, "~~~") // 打印元素
	// 	time.Sleep(1 * time.Second)
	// }
	// fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~~~") // 看不到这一句

	ctx, cancel := context.WithCancel(context.Background())
	go Speak(ctx)
	time.Sleep(5 * time.Second)
	cancel()
	time.Sleep(2 * time.Second)

}

func Speak(ctx context.Context) {
	for range time.Tick(time.Second) {
		select {
		case <-ctx.Done():
			fmt.Println("我要闭嘴了")
			return
		default:
			fmt.Println("balabalabalabala")
		}
	}
}
